package com.woniu.cloud.shopassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TScoreGood;
import com.woniu.cloud.entity.TStoreGood;
import com.woniu.cloud.shopassembly.mapper.TScoreGoodMapper;
import com.woniu.cloud.shopassembly.service.ITScoreGoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分商品表 服务实现类
 * </p>
 *
 * @author hyy
 * @since 2021-09-22
 */
@Service
@Slf4j
public class TScoreGoodServiceImpl extends ServiceImpl<TScoreGoodMapper, TScoreGood> implements ITScoreGoodService {


    @Resource
    private ITScoreGoodService scoreGoodService;
    @Resource
    private TScoreGoodMapper scoreGoodMapper;

    @Override
    public TScoreGood updateRedis(Integer scoreGoodId) {
        log.info("缓存修改->id:{}",scoreGoodId);
        TScoreGood good = scoreGoodMapper.selectById(scoreGoodId);
        log.info("修改结果->{}",good);
        return good;
    }
    @Override
    public TScoreGood insertRedis(Integer scoreGoodId) {
        TScoreGood good=new TScoreGood();
        log.info("缓存添加->id:{}",scoreGoodId);
        good = scoreGoodMapper.selectById(scoreGoodId);
        log.info("缓存结果->{}",good);
        return good;
    }


    @Override
    public TScoreGood getScoreGoodByScoreGoodId(Integer scoreGoodId) {
        log.info("正在查询->scoreGoodId:{}",scoreGoodId);
        TScoreGood scoreGood = scoreGoodService.insertRedis(scoreGoodId);
        return scoreGood;
    }

    @Override
    public Result getScoreGoodByGoodName(String scoreGoodName, Integer pageIndex) {
        log.info("正在查询->scoreGoodName:{},pageIndex:{}", scoreGoodName,pageIndex);
        IPage<TScoreGood> page = new Page<TScoreGood>(pageIndex,6);
        QueryWrapper<TScoreGood> wrapper = new QueryWrapper<>();
        wrapper.eq("score_good_name",scoreGoodName);
        IPage scoreGoodIPage = scoreGoodMapper.selectPage(page,wrapper);
        log.info("查询结果->{}", scoreGoodIPage);
        if (scoreGoodIPage.getTotal()==0L) {
            return Result.fail("数据不存在",null);
        }
        return Result.fail("数据存在",scoreGoodIPage);
    }

//    @Override
//    public Result getScoreGoodByScoreGoodType(String scoreGoodType, Integer pageIndex) {
//        log.info("正在查询->scoreGoodType:{},pageIndex:{}", scoreGoodType,pageIndex);
//        IPage<TScoreGood> page = new Page<TScoreGood>(pageIndex,6);
//        QueryWrapper<TScoreGood> wrapper = new QueryWrapper<>();
//        wrapper.like("score_good_type",scoreGoodType);
//        IPage scoreGoodIPage = scoreGoodMapper.selectPage(page,wrapper);
//        log.info("查询结果->{}", scoreGoodIPage);
//        if (scoreGoodIPage.getTotal()==0L) {
//            return Result.fail("数据不存在",null);
//        }
//        return Result.success("数据存在",scoreGoodIPage);
//    }

    @Override
    public Result insertScoreGood(TScoreGood scoreGood) {
        log.info("正在添加->scoreGood:{}",scoreGood);
        int isInsert = scoreGoodMapper.insert(scoreGood);
        if (1==isInsert) {
            log.info("添加成功->scoreGood:{}",scoreGood);
            scoreGoodService.updateRedis(scoreGood.getScoreGoodId());
            return Result.success("添加成功",null);
        }
        log.info("添加失败->");
        return Result.fail("添加失败",null);
    }

    @Override
    public Result updateScoreGood(TScoreGood scoreGood) {
        log.info("正在修改->scoreGood:{}",scoreGood);
        UpdateWrapper<TScoreGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("score_good_id",scoreGood.getScoreGoodId())
                .set(""!=scoreGood.getScoreGoodName(),"score_good_name",scoreGood.getScoreGoodName())
                .set(0<=scoreGood.getScoreGoodScorePrice(),"score_good_score_price",scoreGood.getScoreGoodScorePrice())
                .set(""!=scoreGood.getScoreGoodImage(),"score_good_image",scoreGood.getScoreGoodImage())
                .set(""!=scoreGood.getScoreGoodDetail(),"score_good_detail",scoreGood.getScoreGoodDetail())
                .set(0<=scoreGood.getScoreGoodStatus(),"score_good_status",scoreGood.getScoreGoodStatus())
                .set(0<=scoreGood.getScoreGoodNum(),"score_good_num",scoreGood.getScoreGoodNum())
                .set(0<=scoreGood.getScoreGoodSellNum(),"score_good_sell_num",scoreGood.getScoreGoodSellNum());
        int update = scoreGoodMapper.update(null, wrapper);
        if (1==update) {
            log.info("修改成功->");
            scoreGoodService.updateRedis(scoreGood.getScoreGoodId());
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);

    }

    @Override
    public Result updateGoodGoodNumByGoodId(Integer scoreGoodId, Integer scoreGoodNum) {
        log.info("正在修改->scoreGoodId:{},scoreGoodNum:{}",scoreGoodId,scoreGoodNum);
        UpdateWrapper<TScoreGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("score_good_id",scoreGoodId)
                .setSql("score_good_num=score_good_num +"+scoreGoodNum);
        int update = scoreGoodMapper.update(null, wrapper);
        if (1==update) {
            log.info("修改成功->");
            scoreGoodService.updateRedis(scoreGoodId);
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);
    }

    @Override
    public Result updateGoodSellNumByGoodId(Integer scoreGoodId, Integer scoreGoodSellNum) {
        log.info("正在修改->scoreGoodId:{},scoreGoodSellNum:{}",scoreGoodId,scoreGoodSellNum);
        UpdateWrapper<TScoreGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("score_good_id",scoreGoodId)
                .setSql("score_good_sell_num=score_good_sell_num +"+scoreGoodSellNum);
        int update = scoreGoodMapper.update(null, wrapper);
        if (1==update) {
            log.info("修改成功->");
            scoreGoodService.updateRedis(scoreGoodId);
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);
    }

    @Override
    public Result updateScoreGoodStatusByScoreGoodId(Integer scoreGoodId,Integer scoreGoodStatus) {
        log.info("正在修改->scoreGoodId:{},scoreGoodStatus:{}",scoreGoodId,scoreGoodStatus);
        UpdateWrapper<TScoreGood> wrapper = new UpdateWrapper<>();
        wrapper.eq("score_good_id",scoreGoodId)
                .set("score_good_status",scoreGoodStatus);
        int update = scoreGoodMapper.update(null, wrapper);
        if (1==update) {
            log.info("修改成功->");
            scoreGoodService.updateRedis(scoreGoodId);
            return Result.success("修改成功",null);
        }
        log.info("修改失败->");
        return Result.fail("修改失败",null);
    }

    @Override
    public Result deleteGoodByGoodId(Integer scoreGoodId) {
        log.info("正在删除->scoreGoodId:{}",scoreGoodId);
        int isDelete = scoreGoodMapper.deleteById(scoreGoodId);
        if (1==isDelete) {
            log.info("删除成功->");
            scoreGoodService.updateRedis(scoreGoodId);
            return Result.success("删除成功",null);
        }
        log.info("删除失败->");
        return Result.fail("删除失败",null);
    }

    @Override
    public TScoreGood getScoreGoodByStoreAndGood(Integer storeId, Integer goodId) {
        QueryWrapper<TScoreGood> tScoreGoodQueryWrapper = new QueryWrapper<>();
        tScoreGoodQueryWrapper.eq("store_good_store_id",storeId);
        tScoreGoodQueryWrapper.eq("store_good_good_id",goodId);
        List<TScoreGood> tScoreGoods = scoreGoodMapper.selectList(tScoreGoodQueryWrapper);
        return tScoreGoods.get(0);
    }

}
